<template>
<!--  <template>-->
<!--    &lt;!&ndash; 显示地图的div &ndash;&gt;-->
<!--    <div id="mapDiv" ref="mapDiv" class="mapDiv">-->
<!--      loading...-->
<!--    </div>-->
<!--  </template>-->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="拼团点名称" prop="title" label-width="70">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入拼团点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="communityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="拼团点ID" align="center" prop="id" />
<!--      <el-table-column label="序号" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.$index + 1 }} &lt;!&ndash; 使用 $index 获取当前行的索引，并加1以从1开始计数 &ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="地址" align="center" prop="address" >
        <template slot-scope="scope">
          <span v-if="scope.row.address.length <= 5">{{ scope.row.address }}</span>
          <span v-else>{{ scope.row.address.substring(0, 5) + '...' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="定位" align="center">
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="navigateToMap(scope.row)">点击定位</span>
        </template>
      </el-table-column>
      <el-table-column label="拼团点名称" align="center" prop="title" >
<!--        在拼团点名称大于五个字的时候，后面的字省略-->
        <template slot-scope="scope">
          <span v-if="scope.row.title.length <= 5">{{ scope.row.title }}</span>
          <span v-else>{{ scope.row.title.substring(0, 5) + '...' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="负责人" align="center" prop="leaderName" />
      <el-table-column label="店铺数量" align="center" prop="storeCount" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="storelist(scope.row)">{{scope.row.storeCount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="拼团数量" align="center" prop="groupCount" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="grouplist(scope.row)">{{scope.row.groupCount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="团员人数" align="center" prop="userCount" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="userlist(scope.row)">{{scope.row.userCount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="未提货" align="center" prop="orderCount1" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="itemlist1(scope.row)">{{scope.row.orderCount1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="已提货" align="center" prop="orderCount0" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="itemlist(scope.row)">{{scope.row.orderCount0}}</span>
        </template>
      </el-table-column>
      <el-table-column label="消费金额" align="center" prop="money" />
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:community:edit']"
          >编辑</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:community:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        @size-change="handleSizeChangea"
        @current-change="handleCurrentChangea"
      />
    </div>

    <!-- 添加或修改拼团点管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="社区名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入社区名称" />
        </el-form-item>
<!--        <el-form-item label="团长外键" prop="leaderId">-->
<!--          <el-input v-model="form.leaderId" placeholder="请输入团长外键" />-->
<!--        </el-form-item>-->
        <el-form-item label="团长" prop="leaderId">
        <el-select
          v-model="form.leaderId"
          clearable
          placeholder="Select"
          style="width: 240px"
        >
          <el-option
            v-for="item in optionssss"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="经度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入纬度" />
        </el-form-item>
<!--        <el-form-item label="地址" prop="address">-->
<!--          <el-input v-model="form.address" placeholder="请输入地址" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommunity, getCommunity, delCommunity, addCommunity, updateCommunity,listLeader } from "@/api/system/community";


export default {
  name: "Community",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拼团点管理表格数据
      communityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        member: null,
        leaderId: null,
        lat: null,
        lng: null,
        address: null
      },
      // 表单参数
      form: {},
      optionssss:[],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.title = this.$route.params.title
    this.getList();
    this.getLeader()
  },
  methods: {
    /** 查询拼团点管理列表 */
    getList() {
      this.loading = true;
      listCommunity(this.queryParams).then(response => {
        this.communityList = response.data.list;
        console.log(this.communityList)
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleSizeChangea(val) {
      this.queryParams.pageSize = val;
      this.getList(); // 假设这是你的数据获取函数
    },
    handleCurrentChangea(val) {
      this.queryParams.pageNum = val;
      this.getList(); // 同样地，假设这是你的数据获取函数
    },

    // 定位跳转////////////////////////////////////////////////////
    navigateToMap(row) {
      this.$router.push({
        name: 'Map',
        params: {
          // user: row.name,
          lat: row.lat,
          lng: row.lng,
          address: row.address
        }
      });
    },
    // 跳转到店铺管理 并且传送id
    storelist(row){
      this.$router.push({
        name: 'StoreMange',
        params: {
          CommunityId: row.id
        }
      });
    },
    // 跳转到拼团管理 并传送id
    grouplist(row){
      this.$router.push({
        name: 'GroupManage',
        params: {
          CommunityId: row.id
        }
      });
    },
    // 跳转到 用户管理 并传送id
    userlist(row){
      this.$router.push({
        name: 'Userb',
        params: {
          CommunityId: row.id
        }
      });
    },
    // 跳转到订单管理 并传送id
    itemlist(row){
      this.$router.push({
        name: 'Ordera',
        params: {
          CommunityId: row.id,
          ItemStatus0: 0
        }
      });
    },
    itemlist1(row){
      this.$router.push({
        name: 'Ordera',
        params: {
          CommunityId: row.id,
          ItemStatus0: 1
        }
      });
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        member: null,
        leaderId: null,
        lat: null,
        lng: null,
        address: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拼团点管理";
    },




    /*
    * 查询全部的leader
    * */
    getLeader() {
      listLeader().then(response => {
        this.optionssss = response.data;
        console.log(this.optionssss)
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCommunity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑拼团点："+this.form.title+"(ID "+this.form.id+")";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCommunity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommunity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除拼团点管理编号为"' + ids + '"的数据项？').then(function () {
        return delCommunity(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/community/export', {
        ...this.queryParams
      }, `community_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
